﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GameSPK
{
    /// <summary>
    /// Interaction logic for frmOChuContent.xaml
    /// </summary>
    public partial class frmOChuContent : Window
    {
        public frmOChuContent()
        {
            InitializeComponent();
            ochuPhanthi = new OChu();
            index = ochudanhap = 0;
            sohangngang = int.Parse(txtNumHangNgang.Text.Trim());
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            btnTruoc.Content = @"<<";
        }
        public OChu ochuPhanthi;
        int ochudanhap;
        int index;
        int sohangngang;
        private void btnSau_Click(object sender, RoutedEventArgs e)
        {
            if (ochudanhap < sohangngang)
            {
                ochudanhap++;
                
                if (index < ochudanhap)
                {
                    #region "check ivalidate"
                    if (txtCauHoi.Text.Trim() == "")
                    {
                        MessageBox.Show("Nhập Nội Dung câu hỏi");
                        return;
                    }
                    if (txtDapAnHangNgang.Text.Trim() == "")
                    {
                        MessageBox.Show("Nhập Ô Chữ Hàng Ngang");
                        return;
                    }
                    if (txtKeyChar.Text.Trim() == "")
                    {
                        MessageBox.Show("Nhập Các Kí tự là khóa");
                        return;
                    }
                    #endregion
                    HangNgang hangngang = new HangNgang();
                    hangngang.CauHoi = txtCauHoi.Text;
                    hangngang.DapAn = txtDapAnHangNgang.Text.Replace(" ", "").Trim();
                    hangngang.KeyChar = (txtKeyChar.Text.Replace(" ","")).Trim().ToArray();
                    if (!hangngang.isValidate)
                    {
                        MessageBox.Show("Hãy Kiểm Tra Đúng từ khóa và ô chữ");
                        ochudanhap--;
                        return;
                    }
                    ochuPhanthi[index] = hangngang;
                    index++;
                    if (index >= sohangngang)
                    {
                        index = 0;
                        txtCauHoi.Text = ochuPhanthi[index].CauHoi;
                        txtDapAnHangNgang.Text = ochuPhanthi[index].DapAn;
                        txtKeyChar.Text = new string(ochuPhanthi[index].KeyChar);
                    }
                    else
                    {
                        txtCauHoi.Text = "";
                        txtKeyChar.Text = "";
                        txtDapAnHangNgang.Text = "";
                    }
                }
            }
            else
            {
                if (index >= sohangngang)
                {
                    index = 0;
                    txtCauHoi.Text = ochuPhanthi[index].CauHoi;
                    txtDapAnHangNgang.Text = ochuPhanthi[index].DapAn;
                    txtKeyChar.Text = new string(ochuPhanthi[index].KeyChar);
                }
                else
                {
                    HangNgang hangngangupdate = new HangNgang();
                    hangngangupdate.CauHoi = txtCauHoi.Text;
                    hangngangupdate.DapAn = txtDapAnHangNgang.Text.Replace(" ", "");
                    hangngangupdate.KeyChar = txtKeyChar.Text.Replace(" ", "").ToArray();
                    if (!hangngangupdate.isValidate)
                    {
                        MessageBox.Show("Hãy Kiểm Tra Đúng từ khóa và ô chữ");
                        
                        return;
                    }
                    ochuPhanthi[index].CauHoi = hangngangupdate.CauHoi;
                    ochuPhanthi[index].DapAn = hangngangupdate.DapAn;
                    ochuPhanthi[index].KeyChar = hangngangupdate.KeyChar;
                    
                    index++;
                    if (index < sohangngang)
                    {
                        txtCauHoi.Text = ochuPhanthi[index].CauHoi;
                        txtDapAnHangNgang.Text = ochuPhanthi[index].DapAn;
                        txtKeyChar.Text = new string(ochuPhanthi[index].KeyChar);
                    }
                    else
                    {
                        index = 0;
                        txtCauHoi.Text = ochuPhanthi[index].CauHoi;
                        txtDapAnHangNgang.Text = ochuPhanthi[index].DapAn;
                        txtKeyChar.Text = new string(ochuPhanthi[index].KeyChar);
                    }
                }
                

            }
        }

        private void txtNumHangNgang_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (string.IsNullOrEmpty(((System.Windows.Controls.TextBox)sender).Text))
                sohangngang = 10;
            else
            {
                int num = 0;
                bool success = int.TryParse(((System.Windows.Controls.TextBox)sender).Text, out num);
                if (success & num >= 0)
                {
                    ((System.Windows.Controls.TextBox)sender).Text.Trim();
                    sohangngang = num;
                }
                else
                {
                    ((System.Windows.Controls.TextBox)sender).Text = "10";
                    ((System.Windows.Controls.TextBox)sender).SelectionStart = ((System.Windows.Controls.TextBox)sender).Text.Length;
                }
            }
        }

        private void txtKey_TextChanged(object sender, TextChangedEventArgs e)
        {
            //this.txtKey.Text = this.txtKey.Text.ToUpper();
        }

        private void txtDapAnHangNgang_TextChanged(object sender, TextChangedEventArgs e)
        {
            //this.txtDapAnHangNgang.Text = this.txtDapAnHangNgang.Text.ToUpper();
        }

        private void txtKeyChar_TextChanged(object sender, TextChangedEventArgs e)
        {
            //this.txtKeyChar.Text = this.txtKeyChar.Text.ToUpper();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (isFillData() == false)
            {
                MessageBox.Show("Hãy Nhập Đầy Đủ Dữ Liệu");
                return;
            }

            ochuPhanthi.ChuDeOChu = txtChuDe.Text.Trim();
            ochuPhanthi.DapAnOChu = txtKey.Text.Trim();
            ochuPhanthi.SoOHangNgang = sohangngang;
            if (!ochuPhanthi.isValidate())
            {
                MessageBox.Show("Hãy Kiểm tra lại nhập ô chữ chính xác");
                return;
            }
            String folder = Helper.folderOChu;
            String filename = Helper.GenerateKey() + ".xml";
            ochuPhanthi.SaveXML(folder + "/" + filename);
            if (CauHoiDAO.luuCauHoi(MainWindow.cuocthiluachon,3, folder + "/" + filename, "", true))
            {
                this.Close();
            }
            
        }
        private bool isFillData()
        {
            if (this.txtChuDe.Text.Trim() == "")
                return false;
            if (this.txtKey.Text.Trim() == "")
                return false;
            if (ochuPhanthi.OChuHangNgang.Count < int.Parse(txtNumHangNgang.Text))
                return false;
            return true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnTruoc_Click(object sender, RoutedEventArgs e)
        {
            
        }
    }
}
